È«ÃñÕ¨½ð»¨_°ÄÃŵç×ÓÓÎÏ·-appÏÂÔØ

ͼƬ

¥Ô¥Ü¥Ã¥È¥Æ©`¥Ö¥ë

Excel¤Ë¤¢¤ë¥Ô¥Ü¥Ã¥È¥Æ©`¥Ö¥ë¤Î¤è¤¦¤Ê±í¤ò×÷³É

Àý£©¥ì¥¹¥È¥é¥ó¤Ë¤ª¤±¤ë¥Á¥Ã¥×½ðî~¤Î¥Ç©`¥¿
total_bill : ï‹Ê³´ú£¨Ë°Þz£©
tip : ¥Á¥Ã¥×¤Î½ðî~
sex : ÐÔ„e£¨Male,Female£©
smoker : †ËŸŸÕߤ¬¤¤¤¿¤«¤É¤¦¤«£¨Yes,No£©
day : ê×ÈÕ£¨Thur,Fri,Sat,Sun£©
time : •régŽ¡£¨Lunch,Dinner£©
size : ¥°¥ë©`¥×¤ÎÈËÊý

library(reshape2)
data(tips)
head(tips)
##   total_bill  tip    sex smoker day   time size
## 1      16.99 1.01 Female     No Sun Dinner    2
## 2      10.34 1.66   Male     No Sun Dinner    3
## 3      21.01 3.50   Male     No Sun Dinner    3
## 4      23.68 3.31   Male     No Sun Dinner    2
## 5      24.59 3.61 Female     No Sun Dinner    4
## 6      25.29 4.71   Male     No Sun Dinner    4

¥Á¥Ã¥×½ðî~¤ò½ðî~„e¤ËëA¼‰·Ö¤±¤·¤Æ¡¢Ëû¤Î‰äÊý¤È¤Î¥Ô¥Ü¥Ã¥È¥Æ©`¥Ö¥ë¤ò×÷³É

tips$y <- cut(tips$tip, breaks = c(0, 2, 4, 6, 8, 10))  # tips¤òëA¼‰·Ö¤±
table(tips$y)
## 
##  (0,2]  (2,4]  (4,6]  (6,8] (8,10] 
##     78    125     34      5      2

¥Ô¥Ü¥Ã¥È¥Æ©`¥Ö¥ë£¨¶ÈÊý£©¤Î×÷³É

acast(tips, y ~ sex + smoker, length)
## Using y as value column: use value.var to override.
##        Female_No Female_Yes Male_No Male_Yes
## (0,2]         19          9      29       21
## (2,4]         27         19      49       30
## (4,6]          8          4      15        7
## (6,8]          0          1       3        1
## (8,10]         0          0       1        1

¥»¥ë¤´¤È¤ËÖ¸¶¨¤·¤¿‰äÊý¤Î¾tºÍ¤äƽ¾ù¤Ê¤É¤òÇó¤á¤¿¤¤ˆöºÏ¤Ï£¬
value.var¤Ç‰äÊýÃû¤òÖ¸¶¨¤¹¤ë

acast(tips, y ~ sex + smoker, sum, value.var = "total_bill")
##        Female_No Female_Yes Male_No Male_Yes
## (0,2]      244.9     106.21  374.21   345.33
## (2,4]      484.4     354.37  943.02   742.74
## (4,6]      248.3     104.52  432.20   175.02
## (6,8]        0.0      28.17  121.99    23.17
## (8,10]       0.0       0.00   48.33    50.81

3´ÎÔª¤Î¥¯¥í¥¹±í¤Ë¤·¤¿¤¤ˆöºÏ

acast(tips, y ~ sex ~ smoker, sum, value.var = "total_bill")
## , , No
## 
##        Female   Male
## (0,2]   244.9 374.21
## (2,4]   484.4 943.02
## (4,6]   248.3 432.20
## (6,8]     0.0 121.99
## (8,10]    0.0  48.33
## 
## , , Yes
## 
##        Female   Male
## (0,2]  106.21 345.33
## (2,4]  354.37 742.74
## (4,6]  104.52 175.02
## (6,8]   28.17  23.17
## (8,10]   0.00  50.81